package ink.metoo.auto.fishpi.chatroom.handler.redpacket

import ink.metoo.auto.fishpi.call.ChatRoomCall
import ink.metoo.auto.fishpi.chatroom.ChatRooms
import ink.metoo.auto.fishpi.chatroom.Message
import ink.metoo.auto.fishpi.chatroom.RedPacket
import java.util.concurrent.TimeUnit

interface RedPacketHandler {

    val types: Array<RedPacket.Type>

    fun handle(message: Message, redPacket: RedPacket) = Thread {
        TimeUnit.SECONDS.sleep(3L)
        ChatRoomCall.openRedPacket(message.oId!!)
    }.start()

    fun receivedStatusHandle(message: Message, redPacket: RedPacket) {
        val whoGot = message.whoGot
        if (whoGot != null) {
            val whoGotReceivedInfo = redPacket.who.findLast { it.userName == whoGot } ?: return
            message.userName = message.whoGive
            message.time = whoGotReceivedInfo.time
            ChatRooms.console(
                message,
                "【${redPacket.typeEnum.description}】 ${redPacket.msg} ${redPacket.money}(${redPacket.got}/${redPacket.count}) <${whoGotReceivedInfo.userName}> ${whoGotReceivedInfo.userMoney}"
            )
        } else {
            val receivedInfo = redPacket.who.joinToString(", ", prefix = "[", postfix = "]") { "<${it.userName}> ${it.userMoney}" }
            ChatRooms.console(
                message,
                "【${redPacket.typeEnum.description}】 ${redPacket.msg} ${redPacket.money}(${redPacket.got}/${redPacket.count}) $receivedInfo"
            )
        }
    }

}


